package common.world
{
	
	import common.manager.GScene;
	import common.utils.ScrollUtil;
	import common.world.layer.Backgound;
	import common.world.layer.EntityLy;
	import common.world.layer.WeatherLy;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 * 游戏世界
	 * @author liuyong 
	 */
	public class World 
	{
		static public var EVENT_MOUSECLICK : String = "EVENT.MOUSECLICKWORLDMAP";
		
		static private var m_root : Sprite;
		
		static private var m_background : Backgound;
		static private var m_entityLayer : EntityLy;
		static private var m_weather : WeatherLy;
		
		static private var m_drag : Boolean;
		static private var m_dragRect : Rectangle;
		
		private static var m_followUtil : ScrollUtil; 
			
		private static var m_worldWidth : Number;
		private static var m_worldHeight : Number;
		
		public function World()
		{
	
		}
		
		static public function build(root:Sprite,worldWidth:Number,worldHeight:Number) : void
		{
			m_root = root;
			
			m_worldWidth = worldWidth;
			m_worldHeight = worldHeight;
			
			m_background = new Backgound;
			m_entityLayer = new EntityLy;
			m_weather = new WeatherLy;
			
			m_followUtil = new ScrollUtil;
			m_followUtil.bind(root,new Rectangle(0,0,GScene.sceneWidth,GScene.sceneHeight),worldWidth,worldHeight);
			
			root.addChild(m_background);
			root.addChild(m_entityLayer);
			root.addChild(m_weather);
		}
		
		static public function destory() : void
		{
			m_background.destory();
			m_entityLayer.destory();
			m_weather.destory();
			
			if(m_drag)
			{
				stopDrag();
			}
			
			m_root = null;
			m_background = null;
			m_entityLayer = null;
			m_weather = null;
			m_dragRect = null;
			m_drag = false;
		}
		
		static public function get EntityLayer() : EntityLy
		{
			return m_entityLayer;
		}
		
		static public function get WeatherLayer() : WeatherLy
		{
			return m_weather;
		}
		
		static public function get Background() : Backgound
		{
			return m_background;
		}
		
		/**
		 * @return 游戏世界地图的宽度
		 */		
		static public function get worldWidth() : Number
		{
			return m_background.width;
		}
		
		static public function set worldWidth(width:Number) : void
		{
			m_background.width = width;
		}
		
		/**
		 * @return 游戏世界地图的高度
		 */	
		static public function get worldHeight() : Number
		{
			return m_background.height;
		}
		
		static public function set worldHeight(height:Number) : void
		{
			m_background.height =  height;	
		}
		
		/**
		 * 设置游戏世界背景图片
		 * @param url 
		 */		
		static public function set BackgroundURL(url : String) : void
		{
			m_background.url = url;
		}
		
		/**
		 * 设置游戏世界背景图片带马赛克小图的加载的
		 * @param mapURL
		 * @param smallMapURL 
		 */		
		static public function  setBackgroundURL(mapURL:String,smallMapURL:String="") : void
		{
			m_background.setURL(mapURL,smallMapURL);
		}
		
		static public function moveTo(px:Number, py:Number) : void
		{
			m_followUtil.moveTo(px,py);
		}
		
		static public function move(vx:Number, vy:Number) : void
		{
			m_followUtil.move(vx,vy);
		}
		
		static public function startDrag(rect:Rectangle=null) : void
		{
			m_dragRect = rect;
			m_drag = true;

			m_root.addEventListener(MouseEvent.MOUSE_DOWN,OnMouseDown);
			m_root.addEventListener(MouseEvent.MOUSE_UP,OnMouseUp);
		}
		
		/**
		 * 添加实体
		 * @param id
		 * @param entity 
		 */		
		static public function addEntity(id:String,entity:DisplayObject) : void
		{
			m_entityLayer.addEntity(id,entity);	
		}
		
		/**
		 * 获取实体
		 * @param id
		 * @return  
		 */		
		static public function findEntity(id:String) : DisplayObject
		{
			return m_entityLayer.findEntity(id);
		}
		
		/**
		 * 删除单个实体
		 * @param id 
		 */		
		static public function removeEntity(id:String) : void
		{
			m_entityLayer.removeEntity(id);
		}
		
		/**
		 * 删除单个实体
		 * @param id 
		 */
		static public function removeAllEntity() : void
		{
			m_entityLayer.removeAll();
		}
		
		static public function stopDrag() : void
		{
			m_drag = false;
			m_root.removeEventListener(MouseEvent.MOUSE_DOWN,OnMouseDown);
			m_root.removeEventListener(MouseEvent.MOUSE_UP,OnMouseUp);
		}
		
		static private function OnMouseDown(evt : MouseEvent) : void
		{
			m_root.startDrag(false,m_dragRect);
		}
		
		static private function OnMouseUp(evt : MouseEvent) : void
		{
			m_root.stopDrag();
		}
	}
}